package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.work.enums.AnimalMarkStatus;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.enums.HarmlessStatus;
import com.jiantong.work.impl.dao.AnimalMarkDao;
import com.jiantong.work.impl.dao.HarmlessDao;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.model.Harmless;
import com.jiantong.work.model.ReportHandleResume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/25.
 *
 */
@Component
public class HarmlessManager {

    private final HarmlessDao harmlessDao;

    private final ReportHandleResumeDao reportHandleResumeDao;

    private final AnimalMarkDao animalMarkDao;

    @Autowired
    public HarmlessManager(HarmlessDao harmlessDao,
                           AnimalMarkDao animalMarkDao,
                           ReportHandleResumeDao reportHandleResumeDao) {
        this.reportHandleResumeDao = reportHandleResumeDao;
        this.harmlessDao = harmlessDao;
        this.animalMarkDao = animalMarkDao;
    }

    /**
     * 更新无害化单据
     * @param harmless 无害化单据对象
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public Map<String , Object> updateHarmless(Harmless harmless, Integer toStatus){
        Integer fromStatus = harmless.getStatus();
        List<String> earTagNumbers = harmless.getEarTagNumbers();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();
        Boolean flagHarmless;
        if(toStatus != null && !fromStatus.equals(toStatus)){

            harmless.setStatus(toStatus);
            flagHarmless = harmlessDao.update(harmless);
            map.put("flagHarmless",flagHarmless);

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(harmless.getId());
            reportHandleResume.setReportNumber(harmless.getNumber());
            reportHandleResume.setType(DocumentsType.HARMLESS.value());
            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

            if(harmless.getReportType().equals(DocumentsType.QUARANTINE_ORIGIN_AREA.value())
                    && toStatus.equals(HarmlessStatus.FINISHED.value()) && earTagNumbers != null){
                Map<String , Object> map1 = Maps.newHashMap();
                map1.put("markNumbers",earTagNumbers);
                map1.put("status", AnimalMarkStatus.USED.value());
                map1.put("outedAt",new Date());
                animalMarkDao.updateMarks(map1);
            }
        }else {
            flagHarmless = harmlessDao.update(harmless);
            map.put("flagHarmless",flagHarmless);
        }
        return map;
    }
    public Boolean update(Harmless harmless) {
        return harmlessDao.update(harmless);
    }
}
